//
//  SEGServiceHotLineViewModel.m
//  Commom
//
//  Created by 梁学彰 on 2020/6/1.
//

#import "SEGServiceHotLineViewModel.h"
#import "SEGFindDictionaryInfoManagr.h"
#import "SEGPlatformHeader.h"
#import "SEGNewAlertView.h"

@interface SEGServiceHotLineViewModel ()

@end

@implementation SEGServiceHotLineViewModel


- (void)loadData {
    WeakSelf;
    RACSignal * signalSelectStewardPhone = [RACSignal createSignal:^RACDisposable * _Nullable(id<RACSubscriber>  _Nonnull subscriber) {
        [self.manager hotline_selectStewardPhoneWithFinished:^(BOOL isSuccessed, id result, NSString *codeType, NSString *errorMessage) {
            [subscriber sendNext:result];
            [subscriber sendCompleted];
        }];
        return nil;
    }];
    
    RACSignal *signalServiceHotLine = [RACSignal createSignal:^RACDisposable * _Nullable(id<RACSubscriber>  _Nonnull subscriber) {
        [self.manager getServiceHotLineWithFinished:^(BOOL isSuccessed, id result, NSString *codeType, NSString *errorMessage) {
            if ([result isKindOfClass:[NSDictionary class]] && [result[@"data"] isKindOfClass:[NSString class]]) {
                NSString *hotLine = result[@"data"];
                if (hotLine.length > 0 ) {
                    weakSelf.telString = hotLine;
                    [subscriber sendNext:hotLine];
                    
                }
            }
            [subscriber sendCompleted];
        }];
        return nil;
    }];
    
    //请求全国监督服务热线电话
    RACSignal *signalPhone = [RACSignal createSignal:^RACDisposable * _Nullable(id<RACSubscriber>  _Nonnull subscriber) {
        NSString *code = [NSString stringWithFormat:@"%@_service_tel",kAppPlatformCode]; //服务热线电话。
        [[SEGFindDictionaryInfoManagr sharedManager] requestDictionaryInfoByCode:code completed:^(BOOL isSuccessed, id  _Nonnull result, NSString * _Nonnull codeType, NSString * _Nonnull errorMessage) {
            if (isSuccessed) {
                if ([result isKindOfClass:[NSDictionary class]] && [result[@"data"] isKindOfClass:[NSDictionary class]] && [result[@"data"][@"result"] isKindOfClass:[NSArray class]]) {
                    NSArray *dataArray = result[@"data"][@"result"];
                    if (dataArray.count > 0 && [[dataArray firstObject] isKindOfClass:[NSDictionary class]]) {
                        NSString *tel = [dataArray firstObject][@"type"];
                        if (tel.length > 0) {
                            weakSelf.phoneString = tel;
                            [subscriber sendNext:tel];
                        }
                    }
                }
            }
            
            [subscriber sendCompleted];
        }];
        return nil;
    }];
    
    self.concatSignal = [RACSignal merge:@[signalSelectStewardPhone,signalServiceHotLine,signalPhone]];
    
}

#pragma mark - getter
- (SEGServiceHotlineManager *)manager {
    if (!_manager) _manager = [SEGServiceHotlineManager new];
    return _manager;
}


@end
